package com.bwhk.splitdata;


import org.apache.commons.codec.binary.StringUtils;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import com.bwhk.splitdata.split.MessageReceiver;
import com.bwhk.splitdata.task.TaskManageJob;


//@Component
//public class SplitWorkerLoad implements ApplicationRunner{

//	@Override
//    public void run(ApplicationArguments var1) throws Exception{
//		ApplicationContext context = SpringContextUtil.getApplicationContext();
//		
//		try {
//            Thread.sleep(1000);  
//        } catch (InterruptedException e) {  
//            // TODO Auto-generated catch block  
//            e.printStackTrace();  
//        }  
//		MessageReceiver r = context.getBean(MessageReceiver.class);
//		
//        System.out.println("SplitWorkerLoad class will be execute when the project was started!");
//    }
@Configuration
public class SplitWorkerLoad implements ApplicationListener<ContextRefreshedEvent> {
	
	protected static Logger logger=LoggerFactory.getLogger(SplitWorkerLoad.class);
	
	@Value("${split.work.num}")
	private int splitWorkNum;
	
	@Value("${is.sender}")
	private String isSender;
	
	private MessageReceiver[] mrs;
	
	@Autowired
	private Scheduler scheduler;
	@Autowired
	private TaskManageJob taskManageJob;
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		//event.getApplicationContext().getBean(arg0)
		
				
		if(StringUtils.equals(isSender, "true")) {
			logger.info("isSender="+isSender);
			// 启动时先加载一遍
			taskManageJob.executeTaskRefresh();
			addSendTask();
		}
		
		mrs = new MessageReceiver[splitWorkNum];
		
		for(int i=0;i<splitWorkNum;i++) {
			mrs[i] = event.getApplicationContext().getBean(MessageReceiver.class,"work"+i);
		}
		
		
		
		
	}
	private void addSendTask() {
			try {
				String newCronExpression = "0 */5 * * * ?";
				String jobName = "JOB_IYW_TASK_MANAGE";
				String triggerName = "TRIGGER_IYW_TASK_MANAGE";
		        JobDetail jobDetail= JobBuilder.newJob(TaskManageJob.class).withIdentity(jobName, "IYW_JOBGROUP_NAME").build();
		        TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
		        triggerBuilder.withIdentity(triggerName, "IYW_TRIGGERGROUP_NAME");
		        triggerBuilder.startNow();
		        triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(newCronExpression));
		        CronTrigger trigger = (CronTrigger) triggerBuilder.build();
		        scheduler.scheduleJob(jobDetail, trigger);  
		        // 启动  
		        if (!scheduler.isShutdown()) {  
		        	scheduler.start();  
		        }  
			} catch (SchedulerException e) {
				logger.error("获得Scheduler失败");
				e.printStackTrace();
			}
	}
	
}
